/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.coreGenerator.base.entitygen.method;

import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.caf.generator.utils.ExpressionUtils;
import com.inspur.edp.cef.api.message.CefException;
import com.inspur.edp.cef.entity.entity.ICefData;
import java.util.ArrayList;
import java.util.Map;
import com.inspur.edp.cef.generator.overall.GeneratorUtil;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.IfStatement;
import org.eclipse.jdt.core.dom.InfixExpression;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
import org.eclipse.jdt.core.dom.ReturnStatement;
import org.eclipse.jdt.core.dom.StringLiteral;
import org.eclipse.jdt.core.dom.ThrowStatement;

public class DataCreateChildGenerator extends ClassMethodGenerator {

  private static String varChildCode = "childCode";
  private java.util.HashMap<String, String> childTypes;

  public DataCreateChildGenerator(java.util.HashMap<String, String> childTypes) {
    this.childTypes = childTypes;
  }

  @Override
  protected boolean getIsOverride() {
    return true;
  }

  @Override
  protected String getMethodName() {
    return "innerCreateChild";
  }

  @Override
  protected TypeInfo getReturnType() {
    return new TypeInfo(ICefData.class.getTypeName());
  }


  @Override
  protected ArrayList<Modifier.ModifierKeyword> getAccessModifiers() {
    ArrayList<Modifier.ModifierKeyword> list = new ArrayList<>();
    list.add(ModifierKeyword.PROTECTED_KEYWORD);
    return list;
  }

  @Override
  protected ArrayList<ParameterInfo> getParameterCollection() {
    ArrayList<ParameterInfo> list = new ArrayList<>();
    ParameterInfo parameterInfo = new ParameterInfo();
    parameterInfo.setParamType(new TypeInfo(String.class.getTypeName()));
    parameterInfo.setParamName(varChildCode);
    list.add(parameterInfo);
    return list;
  }

  @Override
  protected Block buildMethodBody() {
    Block block = ast.newBlock();
    addBlockIfStatements(block);
    addThrowNotSupportChildCodeException(block);
    return block;
  }

  private void addBlockIfStatements(Block block) {
    for (Map.Entry<String, String> item : childTypes.entrySet()) {
      IfStatement ifStatement = ast.newIfStatement();
      addIfExpression(ifStatement, item);
      addIfThenStatement(ifStatement, item);
      block.statements().add(ifStatement);
    }
  }

  private void addIfExpression(IfStatement ifStatement, Map.Entry<String, String> item) {
    MethodInvocation methodInvocation = ast.newMethodInvocation();
    methodInvocation.setExpression(ast.newSimpleName(varChildCode));
    methodInvocation.setName(ast.newSimpleName("equals"));
    methodInvocation.arguments().add(ExpressionUtils.getStringLiteral(ast, item.getKey()));
    ifStatement.setExpression(methodInvocation);
//		InfixExpression infixExpression=ast.newInfixExpression();
//		infixExpression.setOperator(InfixExpression.Operator.EQUALS);
//		infixExpression.setLeftOperand(ast.newSimpleName(varChildCode));
//		infixExpression.setRightOperand(ExpressionUtils.getStringLiteral(ast, item.getKey()));
//		ifStatement.setExpression(infixExpression);
  }

  private void addIfThenStatement(IfStatement ifStatement, Map.Entry<String, String> item) {
    Block block = ast.newBlock();
    ReturnStatement returnStatement = ast.newReturnStatement();
    ClassInstanceCreation classInstanceCreation = ast.newClassInstanceCreation();
    classInstanceCreation.setType(ast.newSimpleType(ast.newName(item.getValue())));
    returnStatement.setExpression(classInstanceCreation);
    block.statements().add(returnStatement);
    ifStatement.setThenStatement(block);
  }

  private void addThrowNotSupportChildCodeException(Block block) {
    addImport(CefException.class.getTypeName());
    ThrowStatement throwStatement = GeneratorUtil.getThrowStatement(ast,"不存在子表",varChildCode,true);
//    ClassInstanceCreation classInstanceCreation = ast.newClassInstanceCreation();
//    classInstanceCreation.setType(
//        new com.inspur.edp.caf.generator.baseInfo.TypeInfo(RuntimeException.class).getType(ast));
//
//    InfixExpression infixExpression = ast.newInfixExpression();
//    infixExpression.setOperator(InfixExpression.Operator.PLUS);
//    StringLiteral stringLiteral = ast.newStringLiteral();
//    stringLiteral.setLiteralValue("不存在子表");
//    infixExpression.setLeftOperand(stringLiteral);
//    infixExpression.setRightOperand(ast.newSimpleName(varChildCode));
//    classInstanceCreation.arguments().add(infixExpression);
//    throwStatement.setExpression(classInstanceCreation);
    block.statements().add(throwStatement);
  }

}
